﻿using MotionControl.Modules.LightSource.Views;
using MotionControl.Modules.PLC.Views;
using MotionControl.Modules.RestService.Views;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System.Windows.Controls;
using MotionControl.Views;
using Prism.Ioc;
//using MotionControl.Modules.RestService;
using System.Diagnostics;
using System.Net.Http.Headers;
using Prism.Events;
using MotionControl.Core;
using System.Windows;

namespace MotionControl.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private const string initName = "Motion Control";
        private string _title = initName;
        private readonly IEventAggregator _eventAgg;
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
        public DelegateCommand<string> ShowContentCmd { get; set; }
        public DelegateCommand WindowClosed { get; set; }

        private readonly IRegionManager _regionManager;
        public MainWindowViewModel(IRegionManager regionManager, IEventAggregator aggregator)
        {
            _regionManager = regionManager;
            _eventAgg = aggregator;

            ShowContentCmd = new DelegateCommand<string>(ShowContentFunc);
            WindowClosed = new DelegateCommand(OnWindowClosed);

            //_regionManager.RegisterViewWithRegion<PLCView>("ContentRegion");
            //_regionManager.RegisterViewWithRegion<RestServiceView>("ContentRegion");
            //_regionManager.RegisterViewWithRegion<LightSourceView>("ContentRegion");

            _regionManager.RegisterViewWithRegion("TabRegion", typeof(PLCView));
            _regionManager.RegisterViewWithRegion("TabRegion", typeof(RestServiceView));
            _regionManager.RegisterViewWithRegion("TabRegion", typeof(LightSourceView));
        }

        private void ShowContentFunc(string name)
        {
            _eventAgg.GetEvent<RestfulMsgEvent>().Publish("Start");
            Title = "运动控制" + "-" + name;
            string viewName = "";
            switch(name)
            {
                case "主界面":
                    viewName = "RestServiceView";
                    break;
                case "PLC设置":
                    viewName = "PLCView";
                    break;
                case "光源设置":
                    viewName = "LightSourceView";
                    break;
            }    
            _regionManager.Regions["ContentRegion"].RequestNavigate(viewName);     
        }

        private void OnWindowClosed()
        {
            _eventAgg.GetEvent<MainWindowEvent>().Publish("Closed");
        }
    }
}
